% reads in Ds for fractional crystallizaiton
close all
clear all


% designate worksheet name to save results:
worksheetName2Save='Ds_FracCryst45_Test';

disp('      ')
disp('      ')
disp('      ')
disp(sprintf('<strong> \n Importing the Ds in file ''AAA_PETROGEN_SourcesAndDs.xlsx''\n in sheet ''TraceElementOrder''\n bewteen the tags ''FirstD_FC''</strong>'))

 openfile = input(sprintf('\n Do you need me to open that file for you? \n Enter ''Y'' if so, any key to continue: '),'s');
 if strcmp(openfile,'Y')
    disp('Okay! Will try...')
     
        if ismac
            % Code to run on Mac platform
             macopen('AAA_PETROGEN_SourcesAndDs.xlsx');
        elseif isunix
            % Code to run on Linux platform
        elseif ispc
            % Code to run on Windows platform
           winopen('AAA_PETROGEN_SourcesAndDs.xlsx');
        else
            disp('Platform not supported')
        end
        
        disp('You need to rerun ''AAA_Master_MeltPlot_Petrogen'' now after saving the spreadsheet. Exiting.')
        return
 end
        
 
disp('      ')
disp('      ')
disp('      ')       
        
%play(Audio);
promptreponse = input(sprintf('<strong>Will save new Ds to the m-file ''%s''</strong> \n To change enter ''N'' [N]:',worksheetName2Save),'s');
if strcmp(promptreponse,'N')
    worksheetName2Save = input('Okay, what should it be?','s');
end



%%
% Import matrix with trace element order and labels
[xlsNumbers, xlsText,xlsRAW] = xlsread('AAA_PETROGEN_SourcesAndDs', 'TraceElementOrder');
[ElementRow,ElementColumn]= find(strcmp(xlsRAW,'Elements')==1);
[LastElementRow,LastElementColumn]= find(strcmp(xlsRAW,'LastElement')==1);

% Import matrix with constant partition coefficients
[DRow,DColumn]= find(strcmp(xlsRAW,'FirstD_FC')==1);
[LastDRow,LastDColumn]= find(strcmp(xlsRAW,'LastD_FC')==1);
Dmatrix = xlsRAW(ElementRow+1:LastElementRow-1, DColumn:LastDColumn);
Dmatrix = cell2mat(Dmatrix);

subfolder = 'Imported datasets';
save([pwd '/' subfolder '/' worksheetName2Save '.mat'],'Dmatrix')

disp(sprintf('\n Finished \n <strong> !!! If you changed the name of the .mat file \n you need to update it in ''AAA_Master_MeltPlot_Petrogen'' \n in variable ''Filename_Ds_FracCryst''</strong>'))
